package cn.xiaobai.service;

import cn.hutool.core.util.NumberUtil;
import cn.xiaobai.domain.Message;
import cn.xiaobai.enums.ServerFunEnum;
import cn.xioabai.common.utility.NumberUtils;
import lombok.SneakyThrows;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Objects;


/**
 * 应用主服务
 *
 * @Author yangdaji
 * @Date 2022/11/2 0002
 * @PackageName cn.xiaobai.server
 * @ClassName Application
 */
@Component
public class PcApplication extends AbstractApplication {

    /**
     * 优先级
     */
    private final Integer priority = 1;

    @Autowired
    private RegeditApplication regeditApplication;


    /**
     * 初始化显示信息
     */
    @PostConstruct
    private void init() {
        List<Message> messageList = priorityMessageMap.get(priority);
        messageList.add(new Message().setMessage("提示: 回车可返回到开始!"));
        messageList.add(new Message().setNumber(0).setMessage("退出!"));
        messageList.add(new Message().setNumber(1).setMessage("关闭以太网"));
        messageList.add(new Message().setNumber(2).setMessage("打开以太网"));
        messageList.add(new Message().setNumber(3).setMessage("关闭WIFI"));
        messageList.add(new Message().setNumber(4).setMessage("打开WIFI"));
        messageList.add(new Message().setNumber(5).setMessage("启动网络连接控制面板"));
        messageList.add(new Message().setNumber(6).setMessage("修改hosts文件"));
        messageList.add(new Message().setNumber(7).setMessage("生成防火墙白名单"));
        messageList.add(new Message().setNumber(8).setMessage("添加常用功能到右键"));
        priorityMessageMap.put(priority, messageList);
    }

    /**
     * 展示功能
     */
    @Override
    public void showMessage(Integer priority) {
        System.out.println("请输入选项:");
        super.showMessage(priority);
        System.out.print("请输入:");
        // 监听用户输入
        listenerInput();
    }


    @Override
    @SneakyThrows
    public void listenerInput() {
        Integer anInt;
        List<Integer> messageNumber = this.getMessageNumber(priority);
        // 判断输入的序号是否存在
        String mess = "没有该选项,请重新输入:";
        while (true) {
            // 序号不存在, 重新输入
            String nextStr = sc.nextLine();
            if (StringUtils.isNoneBlank(nextStr) && NumberUtils.isNumber(nextStr)) {
                anInt = NumberUtil.parseInt(nextStr);
                if (messageNumber.contains(anInt)) {
                    break;
                }
            }
            System.out.print(mess);
        }
        // 当存在对应序号时,处理对应功能,并查询展示面板
        ServerFunEnum item = ServerFunEnum.getItem(anInt);
        if (Objects.nonNull(item)) {
            if (item.getNumber() == 8) {
                ServerFunEnum.setAbstractApplication(regeditApplication);
            }
            item.execute();
            showMessage(priority);
        }
    }
}
